﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Setting
{
    public partial class CommonSetting : Form
    {
        public CommonSetting()
        {
            InitializeComponent();
        }


        private void selecter1_MySelectButtonClick_1(object sender, EventArgs e)
        {
            Util.IdNamePair pair = new DIProject.Util.IdNamePair(Int32.MinValue,string.Empty);
            Setting.SellerList sl = new SellerList(ref pair);
            sl.ShowDialog(this);
            selecter1.SelectedText = pair.Name;
            selecter1.SelectedValue = pair.ID;
        }

        private void selecter2_MySelectButtonClick(object sender, EventArgs e)
        {
            DialogResult dr = fbd.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                selecter2.SelectedText = fbd.SelectedPath;
            }
        }

        private void CommonSetting_Load(object sender, EventArgs e)
        {
            btnSaveWaybillsSetting.Enabled = false;
            cbNkldType.DataSource = this.NkldTypes;
            cbPrinterTypes.DataSource = this.BarCodePrinterTypes;
            Util.SettingFile sf = Util.Setting2.GetSetting();
            cbPrinterTypes.SelectedItem = sf.BarcodePrinter.ToString();
            InitZebraEncoding();
            InitPrices();
            InitOrderUnloadFilePath();
        }

        private void selecter2_MyTextChanget(object sender, EventArgs e)
        {
            btnSaveWaybillsSetting.Enabled = true;
        }
        private void InitZebraEncoding()
        {
            System.Collections.Generic.List<string> sl = new List<string>();           
            foreach (EncodingInfo ei in Encoding.GetEncodings())
            {
                sl.Add(ei.Name);
            }
            sl.Sort();
            cbZebraEncoding.DataSource = sl;
            Util.SettingFile sf = Util.Setting2.GetSetting();
            cbZebraEncoding.SelectedItem = sf.ZebraEncoding;
        }
        private void btnSaveWaybillsSetting_Click(object sender, EventArgs e)
        {
            string seller = selecter1.SelectedText;
            if (string.IsNullOrEmpty(seller))
            {
                MessageBox.Show("Требуется выбрать поставщика!");
                selecter1.Focus();
                return;
            }
            string path = selecter2.SelectedText;
            if (string.IsNullOrEmpty(path))
            {
                MessageBox.Show("Требуется выбрать путь к папке с накладными!");
                selecter2.Focus();
                return;
            }
            string s = cbNkldType.Items[cbNkldType.SelectedIndex] as string;
            Util.NkldType nType = (DIProject.Util.NkldType)Enum.Parse(typeof(DIProject.Util.NkldType), s);
            Util.Setting.SetWaybillsPath(seller, path,nType);
            this.Close();
        }

        private void selecter1_MyTextChanget(object sender, EventArgs e)
        {
            DIProject.Controls.Selecter sel = sender as DIProject.Controls.Selecter;
            if (sel != null)
            {
                if (string.IsNullOrEmpty(sel.SelectedText))
                    return;
                string str = sel.SelectedText;
                string path = Util.Setting.GetWaybillsPath(str);
                selecter2.SelectedText = path;
                Util.NkldType nType = Util.Setting.GetWaybillsNkldType(str);
                cbNkldType.SelectedItem = nType.ToString();
            }
        }
        public string[] NkldTypes
        {
            get
            {
                string[] res = Enum.GetNames(typeof(DIProject.Util.NkldType));
                Array.Sort<string>(res);
                return res;
            }
        }
        public string[] BarCodePrinterTypes
        {
            get
            {
                string[] res = Enum.GetNames(typeof(DIProject.Util.BarCodePrinterType));
                Array.Sort<string>(res);
                return res;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Util.SettingFile sf = Util.Setting2.GetSetting();
            Util.BarCodePrinterType pt = (Util.BarCodePrinterType)Enum.Parse(typeof(Util.BarCodePrinterType), (string)cbPrinterTypes.SelectedItem);
            sf.BarcodePrinter = pt;
            sf.ZebraEncoding = (string)cbZebraEncoding.SelectedItem;
            Util.Setting2.Save(sf);
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (fbdp.ShowDialog() == DialogResult.OK)
            {
                tbPricePath.Text = fbdp.SelectedPath;
            }
        }

        private void btnOrderPath_Click(object sender, EventArgs e)
        {
            if (fbdo.ShowDialog() == DialogResult.OK)
            {
                tbOrderPath.Text = fbdo.SelectedPath;
            }
        }
        private void InitPrices()
        {
            string[] priceTypes = Enum.GetNames(typeof(Util.PriceType));
            PriceType.DataSource = priceTypes;
            string[] orderTypes = Enum.GetNames(typeof(Util.OrderType));
            OrderTypes.DataSource = orderTypes;
           // Util.SettingFile sf = Util.Setting2.GetSetting();
            DataSets.Settings.ContragentsDataTable contragents = new DIProject.DataSets.Settings.ContragentsDataTable();
            DataSets.SettingsTableAdapters.ContragentsTableAdapter adapter = new DIProject.DataSets.SettingsTableAdapters.ContragentsTableAdapter();
            adapter.Fill(contragents);
            DataSets.Settings.ContragentsRow ct = contragents.NewContragentsRow();
            ct.Id = Int32.MinValue;
            ct.Name = "Выберите поставщика";
            ct.ShortName = string.Empty;
            contragents.Rows.InsertAt(ct, 0);
            cbContragentsOrder.DisplayMember = "Name";
            cbContragentsOrder.ValueMember = "Id";
            cbContragentsOrder.DataSource = contragents;

        }

        private void cbContragentsOrder_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 sv = (Int32)cbContragentsOrder.SelectedValue;
            if (sv == Int32.MinValue)
                return;
            Util.SettingFile sf = Util.Setting2.GetSetting();
            List<Util.OrderInfo> orders = sf.Orders;
            if (orders == null)
                return;
            foreach (Util.OrderInfo o in orders)
            {
                if (o.ContragentId == sv)
                {
                    PriceType.SelectedItem = o.PriceType.ToString();
                    OrderTypes.SelectedItem = o.OrderType.ToString();
                    tbPricePath.Text = o.PricePath;
                    tbOrderPath.Text = o.OrderPath;
                    txbDeptFiltr.Text = o.Note;
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Int32 sv = (Int32)cbContragentsOrder.SelectedValue;
            if (sv == Int32.MinValue)
                return;
            Util.SettingFile sf = Util.Setting2.GetSetting();
            List<Util.OrderInfo> orders = sf.Orders;
            if (orders == null)
                orders = new List<DIProject.Util.OrderInfo>();
            Util.OrderInfo o1 = null;
            foreach(Util.OrderInfo o in orders)
            {
                if (o.ContragentId == sv)
                {
                    o1 = o;
                    break;
                }
            }

            if (o1 != null)
                orders.Remove(o1);
            o1 = new DIProject.Util.OrderInfo();
            o1.ContragentId = sv;
            o1.OrderPath = tbOrderPath.Text;
            o1.PricePath = tbPricePath.Text;
            string tmp = (string)OrderTypes.SelectedItem;
            o1.OrderType = (Util.OrderType)Enum.Parse(typeof(Util.OrderType), tmp);
            tmp = (string)PriceType.SelectedItem;
            o1.PriceType =(Util.PriceType)Enum.Parse(typeof(Util.PriceType), tmp);
            o1.Note = txbDeptFiltr.Text;
            orders.Add(o1);
            sf.Orders = orders;
            Util.Setting2.Save(sf);
            //this.Close();
        }

        private void txbDeptFiltr_Validating(object sender, CancelEventArgs e)
        {
            string str = txbDeptFiltr.Text;
            if (string.IsNullOrEmpty(str))
            {
                e.Cancel = true;
                MessageBox.Show("Фильтр для подразделения не может быть пустым");
                txbDeptFiltr.Focus();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Util.SettingFile sf = Util.Setting2.GetSetting();
            string fileName = txbOrderUnloadProgrammFilePath.Text;
            if (string.IsNullOrEmpty(fileName))
            {
                MessageBox.Show("Путь к файлу не должен быть пустым");
                return;
            }
            if (!System.IO.File.Exists(fileName))
            {
                MessageBox.Show("Заданного файла не существует");
                return;
            }
            sf.OrderUnloadProgrammFilePath = fileName;
            Util.Setting2.Save(sf);
            
        }
        private void InitOrderUnloadFilePath()
        {
            Util.SettingFile sf = Util.Setting2.GetSetting();
            txbOrderUnloadProgrammFilePath.Text = sf.OrderUnloadProgrammFilePath;
        }

        private void btnOrderUnloadProgrammFilePath_Click(object sender, EventArgs e)
        {
            if (ofdOuterProgramm.ShowDialog() == DialogResult.OK)
            {
                string fileName = ofdOuterProgramm.FileName;
                if (!System.IO.File.Exists(fileName))
                {
                    MessageBox.Show("Файйла не выбрано");
                    return;
                }
                txbOrderUnloadProgrammFilePath.Text = fileName;
            }
        }
    }
}